#!/usr/bin/env bash
# ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
# Function :CentOS7.X  一 对 多复制文件
# Platform :RedHatEL7.x Based Platform
# Version  :1.01
# Date     :2021-08-04
# Author   :mugoLH
# Contact  :houxiaoshuai@baidu.com & hxsaj@126.com
# Address  :https://gitee.com/hxsaj/tools_shell
# ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
#  变量列表 List of common variables
# ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
#  说明：脚本从 ${HOST_FILE} 指定的文件中获取主机名和IP作为 scp 的目的服务器，如需更换其他文件，需保持类hosts文件样式
#       例：
# 192.168.1.10   hosta
# 192.168.1.12   hostb
# 192.168.1.11   hostc

#  获取主机名列的源文件
HOST_FILE=/etc/hosts

#  函数列表 Function list
# ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
info_p(){    printf "\e[1;35;40m%-8s %-12s %-24s %-24s %-12s\e[0m\n" "$(date '+%H:%M:%S')" "${1}" "${2}" "${3}" "${4}" >&1 ;}

# 如果参数为空，则退出
if [ $# -eq 0 ];then
	exit
else
	#  循环远程执行命令
	HostList=$(egrep -v "localhost|^#" ${HOST_FILE} | awk '{print$2}' | grep -v $(hostname -A | awk '{print$1}') | sort | uniq )
	for i in ${HostList[@]};do
		HostIp=$(grep -w ${i} ${HOST_FILE} | grep -v "^#" |awk '{print$1}' | sort | uniq)
		info_p "从本机复制 ${1} ----> " "${i}(${HostIp}):${2}"
		scp -pr ${1} ${i}:${2}
		echo ""
	done
fi
